package com.atguigu.typehandler;

import com.atguigu.bean.EmployeeStatus;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author : leimingchao
 * @version V1.0
 * @Project: mybatis
 * @Package com.atguigu.typehandler
 * @Description: TODO
 * @date Date : 2021年05月23日 13:46
 * 1.实现TypeHandler接口，或者继承BaseTypeHandler
 */
public class MyEnumEmployeeStatusTypeHandler implements TypeHandler<EmployeeStatus> {

    /**
     * 定义当前数据如何保存到数据库中
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setParameter(PreparedStatement ps, int i, EmployeeStatus parameter, JdbcType jdbcType) throws SQLException {
        System.out.println("要保存的状态码："+parameter.getCode());
        ps.setString(i,parameter.getCode().toString());
    }

    @Override
    public EmployeeStatus getResult(ResultSet rs, String columnName) throws SQLException {
        //需要根据从数据库中拿到的枚举状态码返回一个枚举对象
        int code = rs.getInt(columnName);
        System.out.println("从数据库中获取的状态码："+code);
        EmployeeStatus employeeStatus = EmployeeStatus.getEmployeeStatusByCode(code);
        return employeeStatus;
    }

    @Override
    public EmployeeStatus getResult(ResultSet rs, int columnIndex) throws SQLException {
        //需要根据从数据库中拿到的枚举状态码返回一个枚举对象
        int code = rs.getInt(columnIndex);
        System.out.println("从数据库中获取的状态码："+code);
        EmployeeStatus employeeStatus = EmployeeStatus.getEmployeeStatusByCode(code);
        return employeeStatus;
    }

    @Override
    public EmployeeStatus getResult(CallableStatement cs, int columnIndex) throws SQLException {
        //需要根据从数据库中拿到的枚举状态码返回一个枚举对象
        int code = cs.getInt(columnIndex);
        System.out.println("从数据库中获取的状态码："+code);
        EmployeeStatus employeeStatus = EmployeeStatus.getEmployeeStatusByCode(code);
        return employeeStatus;
    }
}
